package com.listener;

import java.util.List;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.entity.Dlyh;

public class OnlineUserListener implements HttpSessionListener{
	
	/**
	 * 创建
	 */ 	
	public void sessionCreated(HttpSessionEvent event) {
		System.out.println("新建session:"+event.getSession().getId());
//		
//		HttpSession session = event.getSession();
//		ServletContext application = session.getServletContext();
//		// 取得登录的用户名
//		List onlineUserList = (List) application.getAttribute("onlineUserList");
//		System.out.println("当前在线人数："+onlineUserList.size());
	}
	
	/**
	 * 销毁
	 */
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		ServletContext application = session.getServletContext();
		// 取得登录的用户名
		Dlyh dlyh = (Dlyh) session.getAttribute("dlyh");
		// 从在线列表中删除用户名
		List onlineUserList = (List) application.getAttribute("onlineUserList");
		for(int i =0;i<onlineUserList.size();i++){
			if(dlyh.equals(onlineUserList.get(i))){
				onlineUserList.remove(i);
			}
		}
		System.out.println(dlyh.getDlyhmc()+"已经退出！");
	}
}
